package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.entity.ServiceWorkOrder;
import org.jeecg.modules.entity.WorkOrder;
import org.jeecg.modules.service.IServiceWorkOrderService;
import org.jeecg.modules.util.IDUtils;
import org.jeecg.modules.vo.ServiceWorkOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/worker/workOrder")
public class ServiceWorkOrderController {
    @Autowired
    private IServiceWorkOrderService iServiceWorkOrderService;

    /**
     * 查询服务型工单
     * @param serviceWorkOrderVo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ServiceWorkOrderVo>> list(ServiceWorkOrderVo serviceWorkOrderVo,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "1") Integer pageSize) {
        Result<IPage<ServiceWorkOrderVo>> result = new Result<IPage<ServiceWorkOrderVo>>();
        //分页类
        Page<ServiceWorkOrderVo> page = new Page<ServiceWorkOrderVo>(pageNo, pageSize);
        //获得查询结果
        IPage<ServiceWorkOrderVo> iPage = iServiceWorkOrderService.findServiceWork(page, serviceWorkOrderVo);
        iPage.setTotal((long) iServiceWorkOrderService.countServiceWork(serviceWorkOrderVo));
        result.setResult(iPage);
        return result;
    }

    /**
     * 新增服务型工单
     * @param workOrder
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<IPage<ServiceWorkOrder>> addApp(WorkOrder workOrder) {
        Result<IPage<ServiceWorkOrder>> result = new Result<IPage<ServiceWorkOrder>>();
        try {
            workOrder = new WorkOrder();
            workOrder.setWorkId(UUID.randomUUID().toString().replace("-", ""));
            workOrder.setWorkNo("g" + System.currentTimeMillis() + IDUtils.createID());
            iServiceWorkOrderService.save(workOrder);
            result.setMessage("新增成功");
        } catch (Exception exce) {
            exce.printStackTrace();
            result.setSuccess(false);
            result.setMessage("新增失败");
        }
        return result;
    }

    /**
     * 修改服务型工单
     * @param workOrder
     * @return
     */
    @RequestMapping(value = "/upd", method = RequestMethod.POST)
    public Result<IPage<ServiceWorkOrder>> updApp(WorkOrder workOrder) {
        Result<IPage<ServiceWorkOrder>> result = new Result<IPage<ServiceWorkOrder>>();
        try {
            iServiceWorkOrderService.updateById(workOrder);
            result.setMessage("修改成功");
        } catch (Exception exce) {
            exce.printStackTrace();
            result.setSuccess(false);
            result.setMessage("修改失败");
        }
        return result;
    }

    /**
     * 删除服务型工单
     * @param workNo
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public Result<IPage<WorkOrder>> delApp(String workNo) {
        Result<IPage<WorkOrder>> result = new Result<IPage<WorkOrder>>();
        try {
            iServiceWorkOrderService.removeById(workNo);
            result.setMessage("删除成功");
        } catch (Exception exce) {
            exce.printStackTrace();
            result.setSuccess(false);
            result.setMessage("删除失败");
        }
        return result;
    }
}
